package com.yimi.policyservice.repository;

import com.yimi.policyservice.entity.base.optr.OptrCompAccount;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface OptrAccountRepository extends JpaRepository<OptrCompAccount,String> {
    List<OptrCompAccount> findByOptrCompIdAndAccountType(String id,int type);

    @Transactional
    @Modifying
    @Query(value = "delete from t_optr_comp_account  where optr_comp_id in ?1",nativeQuery = true)
    void deleteByCompIdIn(List<String> ids);

    @Transactional
    @Modifying
    @Query(value = "delete from t_optr_comp_account  where optr_comp_id = ?1 and account_type=?3 and id not in ?2",nativeQuery = true)
    void deleteByCompIdInAndIdNotIn(String companyId, List<String> ids,int type);

    List<OptrCompAccount> findByAccountIn(List<String> accounts);


    @Transactional
    @Modifying
    @Query(value = "delete from t_optr_comp_account  where optr_comp_id = ?1 and account_type=?2 ",nativeQuery = true)
    void deleteByCompId(String companyId, int type);

    @Query(value = "select count(*) from (   " +
            "                       select id   " +
            "                       from t_policy_social_product   " +
            "                       where optr_comp_id =?1 and account_id = ?2   " +
            "                       union   " +
            "                       select id   " +
            "                       from t_policy_accfund_product   " +
            "                       where optr_comp_id =?1 and account_id =?2   " +
            "   " +
            "                     ) temp" , nativeQuery = true)
    int check(String companyId, String accountId);
}
